package com.satyam.smooth.domain.dao;

import com.satyam.smooth.dao.hibernate.GenericHibernateDAO;
import com.satyam.smooth.dao.hibernate.GenericSessionProvider;
import com.satyam.smooth.domain.Purchase;
import com.satyam.smooth.domain.Vendor;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author srt
 */
public class PurchaseHibernateDAO extends GenericHibernateDAO<Purchase, Long> {

    private Session session = null;

    public PurchaseHibernateDAO(GenericSessionProvider sessionProvider) {
        super(Purchase.class, sessionProvider);
        this.session = sessionProvider.getSession();
    }

    public Purchase getPurchaseByBillNo(Long billNo) {
        Purchase purchase = (Purchase) session.createCriteria(Purchase.class)
                .add(Restrictions.eq("billNo", billNo)).uniqueResult();

        return purchase;
    }

    public List<Purchase> getPurchases(Vendor vendor, Date fromDate, Date toDate) {
        String hql = "from Purchase where vendor = :vendor and onDate between :fromDate and :toDate";
        List<Purchase> purchaseList = session.createQuery(hql)
                .setParameter("vendor", vendor)
                .setParameter("fromDate", fromDate)
                .setParameter("toDate", toDate).list();
        return purchaseList;
    }

}
